package com.example.task1.ui.AllService.traffic;

import android.os.Bundle;
import android.text.Html;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.TrafficCartListBean;
import com.example.task1.bean.TrafficDetailBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.TimeZone;

import retrofit2.Call;

public class TrafficDetailsActivity extends AppCompatActivity {

    private TextView tvName;
    private TextView tvPhone;
    private TextView tvTime;
    private TextView tvAddress;
    private Button btnSelect;
    private TextView tvPlateNo;
    private TextView tvEngineNo;
    private TextView tvType;
    private TextView tvContent;
    private int id;
    private Button btnPlay;
    private int id1;

    @Override
    protected void onRestart() {
        super.onRestart();
        TrafficCartListBean.RowsDTO cartDate = MyApplication.Traffic.cartDate;
        if (cartDate != null) {
            btnSelect.setVisibility(View.GONE);
            tvEngineNo.setText(cartDate.getEngineNo());
            tvPlateNo.setText(cartDate.getPlateNo());
            tvType.setText(cartDate.getType());
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traffic_details);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("智慧交管");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        id = getIntent().getIntExtra("id", 0);
        initView();
        initData();
        business();
    }

    private void business() {
        btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.intent(TrafficDetailsActivity.this, TrafficCartActivity.class, 1);
            }
        });
        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MyApplication.Traffic.cartDate == null) {
                    MyApplication.show("还未选择检测车辆");
                }else {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("userId",MyApplication.user.getUserId());
                    map.put("carId",MyApplication.Traffic.cartDate.getId());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
                    String format = simpleDateFormat.format(new Date());
                    map.put("aptTime",format);
                    map.put("addressId",id);
                    Call<ApiResp> apiRespCall = Api.api().post(MyApplication.TOKEN, "/prod-api/api/traffic/checkCar/apply",Api.getBody(map));
                    Api.send(apiRespCall, ApiResp.class, new CallBack<ApiResp>() {
                        @Override
                        public void success(ApiResp data) {
                            MyApplication.show("预约成功");
                            finish();
                        }

                        @Override
                        public void error(String msg) {

                        }
                    });
                }
            }
        });
    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/traffic/checkCar/place/" + id);
        Api.send(apiRespCall, TrafficDetailBean.class, new CallBack<TrafficDetailBean>() {
            @Override
            public void success(TrafficDetailBean data) {
                TrafficDetailBean.DataDTO rows = data.getData();
                tvAddress.setText("地址：" + rows.getAddress());
                tvPhone.setText("联系方式：" + rows.getPhone());
                tvTime.setText(rows.getRemarks());
                tvName.setText(rows.getPlaceName());
            }

            @Override
            public void error(String msg) {

            }
        });

    }


    private void initView() {
        tvName = (TextView) findViewById(R.id.tv_name);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        tvTime = (TextView) findViewById(R.id.tv_time);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        btnSelect = (Button) findViewById(R.id.btn_select);
        tvPlateNo = (TextView) findViewById(R.id.tv_plateNo);
        tvEngineNo = (TextView) findViewById(R.id.tv_engineNo);
        tvType = (TextView) findViewById(R.id.tv_type);
        tvContent = (TextView) findViewById(R.id.tv_content);
        tvContent.setText(Html.fromHtml("<p style=\\\"color:#191919;font-family:&amp;font-size:100%;font\u0002style:normal;font-weight:400;margin-left:0px;text-align:left;text-decoration:none;t ext-indent:0px;\\\"><span style=\\\"font-size:16px;\\\">1.</span><span style=\\\"fo nt-size:16px;\\\">至少提前两周预约</span><span style=\\\"font-size:16px;\\\">， <span style=\\\"font-size:100%;\\\">预</span>约确定以支付定金为准，定金 20 元(不可退)；拍 照排期以收到定金顺序为准。 </span></p><span style=\\\"background-color:#FFFFFF;color:#191919;font-family:&quot; font-size:16px;font-style:normal;font-weight:400;line-height:1.9;text-decoration:no ne;\\\"></span><p style=\\\"color:#191919;font-family:&amp;font-size:100%;font-styl e:normal;font-weight:400;margin-left:0px;text-align:left;text-decoration:none;text\u0002indent:0px;\\\"><span style=\\\"font-size:16px;\\\">2.检车余款于拍照当日结清：支付宝／ 微信／现金均可。 </span></p><span style=\\\"background-color:#FFFFFF;color:#191919;font-family:&quot; font-size:16px;font-style:normal;font-weight:400;line-height:1.9;text-decoration:no ne;\\\"> </span><p style=\\\"color:#191919;font-family:&amp;font-size:100%;font-sty le:normal;font-weight:400;margin-left:0px;text-align:left;text-decoration:none;text -indent:0px;\\\"><span style=\\\"font-size:16px;\\\">3.如遇个人原因临时变更，请于原定拍 摄时间提前<span style=\\\"font-size:100%;\\\">至少 48 小时</span>与我联系更改，感谢理解； 预约当天无特殊理由取消，订单作废，再次预约重付 20 元定金。</span></p>"));
        btnPlay = (Button) findViewById(R.id.btn_play);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.Traffic.cartDate = null;
    }
}